package com.example.administrator.compileapp.drawer;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.compileapp.MainActivity;
import com.example.administrator.compileapp.R;
import com.example.administrator.compileapp.activity.WelcomeAnimationActivity;
import com.example.administrator.compileapp.databinding.ActivityLoginBinding;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private ActivityLoginBinding binding;
    private boolean isExit = false;
    private boolean isLogn = false;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        ShareSDK.initSDK(this);
        init();

    }

    private void init() {
        binding.backLogin.setOnClickListener(this);
        binding.buttonLogin.setOnClickListener(this);
        binding.qqLogin.setOnClickListener(this);
        binding.weixinLogin.setOnClickListener(this);
        binding.weiboLogin.setOnClickListener(this);


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(LoginActivity.this, "再按一次退出编译原理学习平台", Toast.LENGTH_SHORT).show();
            handler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    public void onClick(View v) {
        String num = String.valueOf(binding.loginEt1.getText());
        String pas = String.valueOf(binding.loginEt2.getText());
        switch (v.getId()) {
            case R.id.qq_login:
                login(new QQ(this));
                break;
            case R.id.back_login:
                finish();
                break;
            case R.id.button_login:
                if (num == null) {
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                } else if (pas == null) {
                    Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (num.equals("123456") && pas.equals("1")) {
                    Toast.makeText(this, "密码正确，请稍后...", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, WelcomeAnimationActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                Toast.makeText(this, "密码错误，请重新填入", Toast.LENGTH_SHORT).show();
                break;
        }
        case R.id.weixin_login:
        login(new Wechat(this));
        break;
        case R.id.weibo_login:
        login(new SinaWeibo(this));
        break;
    }
}

    private void login(Platform platform) {
        // String userId = platform.getDb().getUserId();
        //Log.d("1620", userId + "userId");
//        if (userId != null) {
//            Toast.makeText(this, "已经登录过", Toast.LENGTH_SHORT).show();
//        } else {
        platform.setPlatformActionListener(new PlatformActionListener() {
            //   Platform.ACTION_AUTHORIZING: 1 要功能不要数据
            // Platform.ACTION_USER_INFOR : 2 要数据不要功能
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Log.d("1620", "当前线程" + Thread.currentThread().getName());
                switch (i) {
                    case Platform.ACTION_AUTHORIZING:
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        finish();
                        Log.d("1620", "onComplete:第三方登录成功，要功能不要数据");
                        break;
                    case Platform.ACTION_USER_INFOR:
                       String nickname = (String) hashMap.get("nickname");
                       String figureurl_qq_1 = (String) hashMap.get("figureurl_qq_2");
                        Intent intent = new Intent(LoginActivity.this, WelcomeAnimationActivity.class);
                        intent.putExtra("nickName",nickname);
                        intent.putExtra("figureurl_qq_1",figureurl_qq_1);
                        startActivity(intent);
                        finish();
                        Set<Map.Entry<String, Object>> entries = hashMap.entrySet();
                        for (Map.Entry<String, Object> entriy : entries) {
                            String key = entriy.getKey();
                            Object value = entriy.getValue();
                            Log.d("1620", "onComplete:第三方登录成功，要数据不要功能," + "key:" + key + ",value" + value);
                        }
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.d("1620", "登录错误");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Log.d("1620", "登录取消");
            }
        });

        //  platform.authorize();  // 要功能不要数据
        platform.showUser(null);   //要数据不要功能
    }

}
